package com.kuanter.kuanterauto.activity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.ChargeAccountInfoJinEAdapter;
import com.kuanter.kuanterauto.adapter.OtherAccountJinEAdapter;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.AccountInfoNew;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.kuanter.kuanterauto.utils.UtilListView;

public class Recharge2Activity extends BaseActivity_NoBar {
	private ListView otherAccountJineList;
	private Intent intent = new Intent();
	protected List<CardInfo> cardInfos;
	public static String RECHARGE_KIND = "RECHARGE_KIND";
	public static String RECHARGE_KIND_WAP = "alipayWAP";
	public static String RECHARGE_KIND_APP = "alipayAPP";
	public static String RECHARGE_KIND_UNIPAY = "unipay";
	private String xuNiAccountCardNumber = "";
	protected ListView chongzhiAccountJineList;
	public static String XuNiAccountCardNumber = "XuNiAccountCardNumber";
	
	@Override
	protected void onResume() {
		super.onResume();
		if(ConfirmRechargeJinEActivity.isChargeSuccess){
			downLoadCardInfos();
			ConfirmRechargeJinEActivity.isChargeSuccess = false;
		}else {
			ConfirmRechargeJinEActivity.isChargeSuccess = false;
		}
	}
	/**
	 * 请求卡片
	 */
	Handler handlerQueryAccountInfo = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
//				String result = msg.getData().getString("result");
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status	// 字段值是否为false
							com.alibaba.fastjson.JSONObject jsonObjectResult = JSON.parseObject(result);
							if(null != jsonObjectResult.getString("userAccountInfo")){
								String JsonStringUserAccountInfo = jsonObjectResult.getString("userAccountInfo");
								com.alibaba.fastjson.JSONObject jsonObjectUserAccountInfo = JSON.parseObject(JsonStringUserAccountInfo);
								//获取其他账户信息
								 JSONArray JsonArrayOtherAccountInfo = jsonObjectUserAccountInfo.getJSONArray("otherAccountInfo");
								
								/* JSONArray JsonArrayOtherAccountInfoIndex = (JSONArray) JsonArrayOtherAccountInfo.get(0);
								 String kahao = (String) JsonArrayOtherAccountInfoIndex.get(2);
								 Log.i("wuyang", kahao);*/
								 List<AccountInfoNew> accountInfoNewList = new ArrayList<AccountInfoNew>();
								 for(int i=0;i<JsonArrayOtherAccountInfo.size();i++){
									 AccountInfoNew accountInfoNew = new AccountInfoNew();
									 JSONArray JsonArrayOtherAccountInfoIndex = (JSONArray) JsonArrayOtherAccountInfo.get(i);
									 //数据  值
									 accountInfoNew.setNumber(String.valueOf((BigDecimal)JsonArrayOtherAccountInfoIndex.get(0)));
									 //数据  单位
									 accountInfoNew.setUnit((String) JsonArrayOtherAccountInfoIndex.get(1));
									 //数据  卡号
									 accountInfoNew.setCardNumber((String) JsonArrayOtherAccountInfoIndex.get(2));
									 //数据  消费类型
									 accountInfoNew.setConsumptionStyle((String) JsonArrayOtherAccountInfoIndex.get(3));
									 accountInfoNewList.add(accountInfoNew);
								 }
								otherAccountJineList = (ListView) findViewById(R.id.otherAccountJineList);
								if(accountInfoNewList.size() == 0){
									tv_other_account.setVisibility(View.INVISIBLE);
								}else {
									otherAccountJineList.setAdapter(new OtherAccountJinEAdapter(Recharge2Activity.this,accountInfoNewList));
									UtilListView.setListViewHeightBasedOnChildren(otherAccountJineList);
								}
								//获取充值账户信息
								 JSONArray JsonArrayreChargeAccountInfo = jsonObjectUserAccountInfo.getJSONArray("rechargeAccountInfo");
								 List<AccountInfoNew> ChargeAccountInfoNewList = new ArrayList<AccountInfoNew>();
								 for(int i=0;i<JsonArrayreChargeAccountInfo.size();i++){
									 AccountInfoNew accountInfoNew = new AccountInfoNew();
									 JSONArray JsonArrayChargeAccountInfoIndex = (JSONArray) JsonArrayreChargeAccountInfo.get(i);
									 //数据  值
									 accountInfoNew.setNumber(String.valueOf((BigDecimal)JsonArrayChargeAccountInfoIndex.get(0)));
									 //数据  单位
									 accountInfoNew.setUnit((String) JsonArrayChargeAccountInfoIndex.get(1));
									 //数据  卡号
									 accountInfoNew.setCardNumber((String) JsonArrayChargeAccountInfoIndex.get(2));
									 //数据  消费类型
									 accountInfoNew.setConsumptionStyle((String) JsonArrayChargeAccountInfoIndex.get(3));
									 ChargeAccountInfoNewList.add(accountInfoNew);
								 }
								 if(ChargeAccountInfoNewList.size()>0){
									 for(AccountInfoNew accountInfoNew : ChargeAccountInfoNewList){
										 xuNiAccountCardNumber +=accountInfoNew.getCardNumber()+ "@";
									 }
									 xuNiAccountCardNumber = xuNiAccountCardNumber.substring(0, xuNiAccountCardNumber.lastIndexOf("@"));
								 }else {
									 xuNiAccountCardNumber = "@";
								 }
								 chongzhiAccountJineList = (ListView) findViewById(R.id.chongzhiAccountJineList);
								 chongzhiAccountJineList.setAdapter(new ChargeAccountInfoJinEAdapter(Recharge2Activity.this,ChargeAccountInfoNewList));
								 UtilListView.setListViewHeightBasedOnChildren(chongzhiAccountJineList);
							}
						} else {
							// 显示返回数据中 errorMsg 字段值
							showToast("失败");
						}
					} catch (JSONException e) {
						finishProgress();
						showToast("失败");
						e.printStackTrace();
					}
				} else {
					finishProgress();
					showToast("网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
	private DataLoader dataLoader;
	private TextView xuni_account;
	private TextView tv_other_account;
	private void downLoadCardInfos() {
		dataLoader = new DataLoader(this);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//从SharedPreferences本地化数据中区账号信息
		params.add(new BasicNameValuePair("userId",""+SharePrefenceUtil.getUserID(this)));
		Log.i("wuyang", SharePrefenceUtil.getUserID(this)+"");
		dataLoader.DisplayData(MainConstants.HOST_test+ HttpConfigAPI.QUERY_ACCOUNT_INFO, params, handlerQueryAccountInfo, "", false);
		showProgress("正在加载数据...");
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge2);
		tv_other_account = (TextView)findViewById(R.id.tv_other_account);
		downLoadCardInfos();
	}

	public void alipayWAP(View v) {
		//支付宝网页支付	方法名：cardRecharge/{cardNumber}/{rechargeAmount}/{mobileNumber}/trade		参数名：cardNumber，rechargeAmount，mobileNumber
		intent.setClass(this, ConfirmRechargeJinEActivity.class);
		intent.putExtra(RECHARGE_KIND, Recharge2Activity.RECHARGE_KIND_WAP);
		intent.putExtra(XuNiAccountCardNumber, xuNiAccountCardNumber);
		startActivity(intent);
//		startActivity_anim(0);
	}

	public void alipayAPP(View v) {
		//参数名：cardNumber,rechargeAmount,mobileNumber
		intent.setClass(this, ConfirmRechargeJinEActivity.class);
		intent.putExtra(RECHARGE_KIND, Recharge2Activity.RECHARGE_KIND_APP);
		intent.putExtra(XuNiAccountCardNumber, xuNiAccountCardNumber);
		startActivity(intent);
//		startActivity_anim(0);
	}
	
	public void unionpay(View view) {
		if(ProApplication.state.equals("0")) {
			showToast("该功能尚未开放，请选择其他方式充值");
			//TODO
			/*intent.setClass(this, ConfirmRechargeJinEActivity.class);
			intent.putExtra(RECHARGE_KIND, Recharge2Activity.RECHARGE_KIND_UNIPAY);
			intent.putExtra(XuNiAccountCardNumber, xuNiAccountCardNumber);
			startActivity(intent);*/
		} else if(ProApplication.state.equals("1")) {
			intent.setClass(this, ConfirmRechargeJinEActivity.class);
			intent.putExtra(RECHARGE_KIND, Recharge2Activity.RECHARGE_KIND_UNIPAY);
			intent.putExtra(XuNiAccountCardNumber, xuNiAccountCardNumber);
			startActivity(intent);
//		startActivity_anim(0);
		} else {
			showToast("该功能尚未开放，请选择其他方式充值");
		}
	}
	
	public void back(View v){
		finish();
	}
}
